package com.ewei.web.common.response;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
/**
 * response view object
 * @author David
 *
 */
@JsonInclude(Include.NON_EMPTY)
public class ResponseVO  implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 3535517036135616904L;
	private static final String ERROR_CODE = "88888";
	protected String status = "200";
	protected String contentType="application/json;charset=UTF-8";
	protected Object datas = null;
	public String getStatus() {
		return status;
	}
	public ResponseVO setStatus(String status) {
		this.status = status;
		return this;
	}
	public String getContentType() {
		return contentType;
	}
	public void setContentType(String contentType) {
		this.contentType = contentType;
	}
	public Object getDatas() {
		return datas;
	}
	public ResponseVO setDatas(Object datas) {
		this.datas = datas;
		return this;
	}
	public ResponseVO error(Object datas){
		this.datas = datas;
		this.status = ERROR_CODE;
		return this;
	}
	public ResponseVO error(String code,Object message){
		this.datas = message;
		this.status = code;
		return this;
	}
	/**
	 * 这里对验证结果进行解析
	 * 前端不显示所有错误
	 * @param result
	 * @param config
	 * @return
	 */
	public ResponseVO resolveError(BindingResult result){
		List<ObjectError> errors = result.getAllErrors();
		this.status = ERROR_CODE;
		Map<String,String> errorsMap = new HashMap<String,String>();
		for(ObjectError error : errors){
			errorsMap.put(error.getCode(),error.getDefaultMessage());
		}
		this.datas = errorsMap;
		return this;
	}
}
